package csu.web.credit_bank.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import csu.web.credit_bank.pojo.Resource;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import csu.web.credit_bank.pojo.vo.ResourceVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


@Repository
public interface ResourceMapper extends BaseMapper<Resource> {

    // 分页查询资源列表，并关联查询发布者的用户名
    @Select("SELECT r.*, u.username AS publisherName " +
            "FROM resources r LEFT JOIN users u ON r.publisher_id = u.id " +
            "${ew.customSqlSegment}")
    IPage<ResourceVO> selectResourcePageWithUsername(Page<ResourceVO> page, @Param(Constants.WRAPPER) QueryWrapper<Resource> queryWrapper);

    // 根据ID查询单个资源，只关联查询发布者的用户名 (不查询头像)
    @Select("SELECT r.*, u.username AS publisherName " +
            "FROM resources r LEFT JOIN users u ON r.publisher_id = u.id " +
            "WHERE r.id = #{id}")
    ResourceVO selectResourceWithUsernameById(String id);
}